
const jwt = require('jsonwebtoken')
const secret = "ultraman"//解密密钥

const JWT = {
    createToken: (data, time) => {
        return jwt.sign(data, secret, { expiresIn: time })
    },
    verifyToken: (token) => {
        // 如果token过期或验证失败，将返回false
        try {
            return jwt.verify(token, secret)
        } catch (error) {
            return false
        }
    },
    decodeData: (token) => {
        // 如果token过期或验证失败，将返回false
        try {
            let userData = jwt.decode(token)
            return { data: userData, success: true }
        } catch (error) {
            return { data: null, success: false }
        }
    }
}

module.exports = JWT